//
//  MessagePushViewController.m
//  project
//
//  Created by 圆圆陈 on 2017/12/16.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "MessagePushViewController2.h"
#import "TimeSetView.h"
@interface MessagePushViewController2 ()<UITableViewDelegate, UITableViewDataSource>
{
    UIButton *setBtn;
    SNShadowView *shadowLine1;
    SNShadowView *shadowLine2;
    UILabel *label2;
}
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray     *titleArr;
@property(nonatomic, strong) NSArray     *imageArr;
@property(nonatomic, strong) NSString    *timeStr;
@end

@implementation MessagePushViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame           = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame           = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), screenWidth, 44)];
    lable.textColor     = [UIColor blackColor];
    lable.font          = SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text          = HHH_Text(@"消息推送");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    _tableView                 = [[UITableView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20)+44+2, HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20)-44-2) style:UITableViewStylePlain];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _imageArr = @[@"来电提醒", @"短信提醒", @"免打扰开关", @"nnn1",@"nnn2",@"nnn3",@"nnn4",@"nnn5",@"nnn6",@"nnn7",@"nnn8",@"nnn9",@"nnn10",@"nnn11"];
    _titleArr = @[HHH_Text(@"来电提醒"), HHH_Text(@"短信提醒"), HHH_Text(@"免打扰开关"), HHH_Text(@"Instagram"), @"Email", HHH_Text(@"Facebook"), HHH_Text(@"Wechat"), HHH_Text(@"Line"), HHH_Text(@"Weibo"), HHH_Text(@"Linkin"), HHH_Text(@"QQ"), HHH_Text(@"WhatsApp"), HHH_Text(@"Viber"), HHH_Text(@"Others")];
    if (KK_BLEModel.bleAdvID == 3089 || KK_BLEModel.bleAdvID == 1821 || KK_BLEModel.bleAdvID == 11805 || KK_BLEModel.bleAdvID == 23581) {
        _timeStr = SN_Text(@"该功能请在手环上设置");
    } else {
        _timeStr = [NSString stringWithFormat:@"(%02ld:%02ld-%02ld:%02ld)", SN_USER.startH, SN_USER.startM, SN_USER.endH, SN_USER.endM];
    }
    
    
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
    [self.view addSubview:shadowLine];
    
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    checkBtn.frame = CGRectMake(HHHWIDTH/8, HHHHEIGHT - FitH(40) - 30, HHHWIDTH/8*6, FitH(40));
    [checkBtn setBackgroundColor:[UIColor blackColor]];
    [checkBtn setTitleNormal:HHH_Text(@"一键检查推送异常")];
    checkBtn.titleLabel.font = SNFONT(FitFont(20));
    [checkBtn setTitleColorNormal:[UIColor whiteColor]];
    checkBtn.layer.cornerRadius = 5;
    checkBtn.clipsToBounds      = YES;
    [checkBtn addTouchUpTarget:self action:@selector(checkUpdate)];
    //    [self.view addSubview: checkBtn];
}

- (void)checkUpdate {
    //    [KK_BLESend sendUserPairWithBlock:^(id object, KKBLEAcceptType type) {
    //
    //    }];
    [KK_BLESend sendUserInfoSettingWithBlock:^(id object, KKBLEAcceptType type) {
    }];
}

- (void)leftbuttonClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return FitH(60);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell                           = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.selectionStyle            = UITableViewCellSelectionStyleNone;
//        cell.detailTextLabel.textColor = UIColorHEX(0x727272);
    }
    cell.imageView.image              = UIImageNamed(_imageArr[indexPath.row]);
    cell.imageView.layer.cornerRadius = cell.imageView.height/2;
    cell.imageView.clipsToBounds      = YES;
    NSString *title = _titleArr[indexPath.row];
    cell.textLabel.text               = title;
    cell.textLabel.font               = SNFONT(14);
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    
    if ([title isEqualToString:HHH_Text(@"免打扰开关")]) {
        if (setBtn) {
            [setBtn removeFromSuperview];
            setBtn = nil;
        }
        if (shadowLine1) {
            [shadowLine1 removeFromSuperview];
            shadowLine1 = nil;
        }
        if (shadowLine2) {
            [shadowLine2 removeFromSuperview];
            shadowLine2 = nil;
        }
        if (label2) {
            [label2 removeFromSuperview];
            label2 = nil;
        }
        setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        setBtn.frame = CGRectMake(FitW(334)-FitW(50), FitH(5), FitW(50), FitH(60)-FitH(10));
        [setBtn setImageNormal:@"设置1"];
        setBtn.tag = 100+indexPath.row;
        [setBtn addTouchUpTarget:self action:@selector(setBtnClick:)];
        [cell addSubview:setBtn];
        
        if (KK_BLEModel.bleAdvID == 3089 || KK_BLEModel.bleAdvID == 1821 || KK_BLEModel.bleAdvID == 11805 || KK_BLEModel.bleAdvID == 23581) {
            setBtn.enabled = NO;
        } else {
            setBtn.enabled = YES;
        }
        
        label2 = [[UILabel alloc] initWithFrame:SNFRAME(20,FitH(60)-25,150,20)];
        label2.textColor = UIColorHEX(0x727272);
        label2.font = SNFONTMIN(12);
        label2.text = _timeStr;
        label2.adjustsFontSizeToFitWidth = YES;
        label2.textAlignment = NSTextAlignmentLeft;
        [cell addSubview:label2];

        shadowLine1 = [[SNShadowView alloc] init];
        shadowLine1.frame = SNFRAME(0, 0, HHHWIDTH, 0);
        [shadowLine1 showWithType:1];
        [cell addSubview:shadowLine1];
        
        shadowLine2 = [[SNShadowView alloc] init];
        shadowLine2.frame = SNFRAME(0, FitH(60) -1, HHHWIDTH, 0);
        [shadowLine2 showWithType:1];
        [cell addSubview:shadowLine2];
    }
    
    ZJSwitch *btn = [[ZJSwitch alloc] initWithFrame:CGRectMake(HHHWIDTH-50, FitH(10), 50, 22)];
    btn.on = NO;
    btn.tag = 100 +indexPath.row;
    if (indexPath.row == 0) {
        btn.on = SN_USER.phoneNotice;
    } else if (indexPath.row == 1) {
        btn.on = SN_USER.smsNotice;
    } else if (indexPath.row == 2) {
        btn.on = SN_USER.isOpen;
        
        if (KK_BLEModel.bleAdvID == 3089 || KK_BLEModel.bleAdvID == 1821 || KK_BLEModel.bleAdvID == 11805 || KK_BLEModel.bleAdvID == 23581) {
            btn.enabled = NO;
        } else {
            btn.enabled = YES;
        }
    } else if (indexPath.row == 3) {
        btn.on = SN_USER.Instagram;
    }
    else if (indexPath.row == 4) {
        btn.on = SN_USER.Emails;
    }
    else if (indexPath.row == 5) {
        btn.on = SN_USER.Facebook;
    }
    else if (indexPath.row == 6) {
        btn.on = SN_USER.Wechat;
    }
    else if (indexPath.row == 7) {
        btn.on = SN_USER.Line;
    }
    else if (indexPath.row == 8) {
        btn.on = SN_USER.Weibo;
    }
    else if (indexPath.row == 9) {
        btn.on = SN_USER.Linkin;
    }
    else if (indexPath.row == 10) {
        btn.on = SN_USER.QQ;
    }
    else if (indexPath.row == 11) {
        btn.on = SN_USER.WhatsApp;
    }
    else if (indexPath.row == 12) {
        btn.on = SN_USER.Viber;
    }
    else if (indexPath.row == 13) {
        btn.on = SN_USER.Others;
    }

    [btn addTarget:self action:@selector(handleSwitchEvent:) forControlEvents:UIControlEventValueChanged];
    cell.accessoryView = btn;
    
    return cell;
}

- (void)setBtnClick:(UIButton *)sender {
    TimeSetView *setView = [[TimeSetView alloc]initWithFrame:self.view.bounds withType:IgnoreAll];
    
    [self.view addSubview:setView];
    [setView animationUp];
    setView.sureButtonBlock = ^(NSString *str) {
        _timeStr = str;
        NSIndexPath *indexPath  = [NSIndexPath indexPathForRow:2 inSection:0];
        NSArray     *indexArray = [NSArray arrayWithObject:indexPath];
        [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationNone];
    };
}

- (void)handleSwitchEvent:(ZJSwitch *)sender {
    switch (sender.tag) {
        case 100:
            NSLog(@"🙂>>>设置来电");
            SN_USER.phoneNotice = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 101:
            NSLog(@"🙂>>>设置短信");
            SN_USER.smsNotice = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 102:
            SN_USER.isOpen = sender.on;
            NSLog(@"🙂>>>设置勿扰");
            [KK_BLESend sendNotDisturbWithBlock:^(id object, KKBLEAcceptType type) {
            }];
            break;
        case 103:
            NSLog(@"🙂>>>设置instagram");
            SN_USER.Instagram = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 104:
            NSLog(@"🙂>>>email");
            SN_USER.Emails = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 105:
            NSLog(@"🙂>>>设置facebook");
            SN_USER.Facebook = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 106:
            NSLog(@"🙂>>>设置wechat");
            SN_USER.Wechat = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 107:
            NSLog(@"🙂>>>设置Line");
            SN_USER.Line = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 108:
            NSLog(@"🙂>>>设置Weibo");
            SN_USER.Weibo = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 109:
            NSLog(@"🙂>>>设置linkin");
            SN_USER.Linkin = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 110:
            NSLog(@"🙂>>>设置qq");
            SN_USER.QQ = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 111:
            NSLog(@"🙂>>>设置whatsapp");
            SN_USER.WhatsApp = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 112:
            NSLog(@"🙂>>>设置viber");
            SN_USER.Viber = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        case 113:
            NSLog(@"🙂>>>设置others");
            SN_USER.Others = sender.on;
            [SN_EveryDayModelHelp startUpdateSettingOther1];
            break;
        default:
            break;
    }
    [SN_USER updateToDB];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

